package com.acane.instagram

import android.os.Build
import android.os.Bundle
import android.view.View
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.runtime.Composable
import androidx.compose.ui.tooling.preview.Preview
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.compose.rememberNavController
import com.acane.instagram.core.config.WebSocketManager
import com.acane.instagram.core.design.theme.InstagramTheme
import com.acane.instagram.feature.common.SessionManager
import com.acane.instagram.feature.post.PostViewModel
import com.acane.instagram.ui.MyInstagram

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        // 初始化WebSocket管理器
        //WebSocketManager.initialize(this)
        setupLifecycleObserver()
//        // 确保 DecorView 不为空并且使用 WindowInsetsControllerCompat 控制状态栏
//        window.decorView.post {
//            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
//                // 对于 Android 11（API 30）及更高版本
//                window.insetsController?.show(android.view.WindowInsets.Type.statusBars())
//            } else {
//                // 对于较低版本的 Android
//                window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_VISIBLE
//            }
//        }
        //显示到状态栏
        enableEdgeToEdge()


        setContent {
            val navController = rememberNavController()
            val viewModel: PostViewModel = viewModel()
            InstagramTheme {
                MyInstagram(
                    navController = navController,
                )
            }
        }
    }
    private fun setupLifecycleObserver() {
        lifecycle.addObserver(object : DefaultLifecycleObserver {
            // 优化后的生命周期监听
            override fun onStart(owner: LifecycleOwner) {
                SessionManager._sessionId?.let { sessionId ->
                    if (!WebSocketManager.isConnected) {
                        WebSocketManager.connect(sessionId)
                    }
                }
            }

            override fun onResume(owner: LifecycleOwner) {
                WebSocketManager.setAppForegroundState(true)
            }

            override fun onPause(owner: LifecycleOwner) {
                WebSocketManager.setAppForegroundState(false)
            }

//            override fun onStop(owner: LifecycleOwner) {
//                // 根据业务需求决定是否保持后台连接
//                if (shouldKeepConnectionInBackground().not()) {
//                    WebSocketManager.disconnect()
//                }
//            }
        })
    }
    // 添加在MainActivity中的策略判断
//    private fun shouldKeepConnectionInBackground(): Boolean {
//        // 根据业务需求实现逻辑，例如：
//        return preferences.getBoolean("keep_connection", false)
//                && SessionManager.isLoggedIn()
//    }
    // 显示状态栏
     fun showStatusBar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            window.insetsController?.show(android.view.WindowInsets.Type.statusBars())
        } else {
            window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_VISIBLE
        }
    }

    // 隐藏状态栏
//    fun hideStatusBar() {
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
//            window.insetsController?.hide(android.view.WindowInsets.Type.statusBars())
//        } else {
//            window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_FULLSCREEN
//        }
//    }
}



@Preview(showBackground = true)
@Composable
fun GreetingPreview() {
    InstagramTheme {

    }
}